package univ.bme.accounting.domain;


import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter

@Entity
@Access(AccessType.FIELD)
@Table(name="transactions")
public class Transaction implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;
	
	public static final int INCOME = 1;
	public static final int OUTGO = -1;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	@ManyToOne(targetEntity = User.class, fetch = FetchType.LAZY, optional = false )
	@Fetch( FetchMode.SELECT )
	@JoinColumn(name = "user_id")
	private User user;
	
	@ManyToOne(targetEntity = Category.class, fetch = FetchType.LAZY, optional = false )
	@Fetch( FetchMode.JOIN )
	@JoinColumn(name = "category_id")
	private Category category;
	
	@OneToMany(mappedBy = "transaction", cascade = CascadeType.REMOVE)
	private List<Comment> comments;	
	
	@Column
	private double amount;
	
	@Column
	private int type;
	
	@Column
	private Date date;
	
	@Column(length=500)
	private String note;

	public Transaction() {
		super();
		this.date = new Date();
		this.type = INCOME;
	}
	
	public String getFormattedDate(){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy. MM. dd.");
		return formatter.format(date);
	}
	
	public String getTypeString(){
		return this.type == INCOME?"income":"outgo";
	}
	
	public static String getTypeStringByValue(int value){
		return value==1?"income":"outgo";		
	}
	
}
